import tkinter as tk
from tkinter import messagebox

class SocialNetwork:
    def __init__(self, students, communities):
        self.students = students
        self.communities = communities
        self.root = tk.Tk()
        self.setup_ui()

    def setup_ui(self):
        self.root.title("Social Life Systems")

        # 学生列表
        self.student_listbox = tk.Listbox(self.root)
        for student in self.students:
            self.student_listbox.insert(tk.END, student.fullName)
        self.student_listbox.pack(pady=10)

        # 显示朋友按钮
        display_friends_button = tk.Button(self.root, text="Display friends", command=self.display_friends)
        display_friends_button.pack(pady=5)

        # 过滤朋友相关
        interest_label = tk.Label(self.root, text="Filter Friends by interests")
        interest_label.pack(pady=5)
        self.interest_entry = tk.Entry(self.root)
        self.interest_entry.pack(pady=5)
        filter_friends_button = tk.Button(self.root, text="Filter Friends", command=self.filter_friends)
        filter_friends_button.pack(pady=5)

        # 加入社区相关
        self.selected_community = tk.StringVar(self.root)
        self.selected_community.set("Community 1")
        community_menu = tk.OptionMenu(self.root, self.selected_community, "Community 1", "Community 2")
        community_menu.pack(pady=5)
        add_community_button = tk.Button(self.root, text="Join the Community", command=self.add_to_community)
        add_community_button.pack(pady=5)

        # 显示桥梁学生相关
        find_bridges_button = tk.Button(self.root, text="Find the bridges between communities", command=self.find_bridges)
        find_bridges_button.pack(pady=5)

        # 显示朋友列表文本框
        self.friends_text = tk.Text(self.root, height=10, width=50)
        self.friends_text.pack(pady=10)

        # 显示桥梁学生文本框
        self.bridges_text = tk.Text(self.root, height=10, width=50)
        self.bridges_text.pack(pady=10)

    def display_friends(self):
        student_index = self.student_listbox.curselection()
        if student_index:
            student_index = int(student_index[0])
            student = self.students[student_index]
            self.friends_text.delete(1.0, tk.END)
            for friend in student.get_friends():
                self.friends_text.insert(tk.END, friend.fullName + "\n")
        else:
            messagebox.showwarning("Warning", "Please select a student")

    def filter_friends(self):
        student_index = self.student_listbox.curselection()
        if student_index:
            student_index = int(student_index[0])
            student = self.students[student_index]
            interest = self.interest_entry.get()
            filtered_friends = student.filter_friends(interest)
            self.friends_text.delete(1.0, tk.END)
            for friend in filtered_friends:
                self.friends_text.insert(tk.END, friend.fullName + "\n")
        else:
            messagebox.showwarning("Warning", "Please select a student")

    def add_to_community(self):
        student_index = self.student_listbox.curselection()
        if student_index:
            student_index = int(student_index[0])
            student = self.students[student_index]
            community_name = self.selected_community.get()
            for community in self.communities:
                if community_name == f"Community {self.communities.index(community) + 1}":
                    community.add_member(student)
                    messagebox.showinfo("Success", f"{student.fullName} has been added to {community_name}")
                    break
        else:
            messagebox.showwarning("Warning", "Please select a student")

    def find_bridges(self):
        bridges = find_bridges(self.communities)
        self.bridges_text.delete(1.0, tk.END)
        for bridge in bridges:
            self.bridges_text.insert(tk.END, bridge.fullName + "\n")

    def run(self):
        self.root.mainloop()
